<?php
/*
 * 生成分页码
 * param int $num 文章总数
 * param int $curr 当前显示的页码数
 * param int $cnt 每页显示的条数
 *
 */

function getPage($num,$curr,$cnt)
{
    //最大的页码数
    $max = ceil($num / $cnt);
    //最左侧页码
    $left = max(1, $curr - 2);
    //最右侧页码
    $right = min($left + 4, $max);

    $page = array();
    for ($i = $left; $i <= $right; $i++) {
        $_GET['page'] = $i;
        $page[$i] = http_build_query($_GET);
    }
    return $page;
}


$cve = array();

//漏洞分类
@$get = $_GET['cat'];
if(isset($get)){
    if($get == 'CVE') {
        $file = fopen('CVE.csv', 'r');
        while ($data = fgetcsv($file)) {
            if (empty($data[0])) {
                continue;
            }
            $data[4] = $data[0];
            array_push($cve, $data);
            //array_push($vv2,$data);
        }
    }
    else if($get == 'seebug'){
            $file = fopen('seebug.csv','r');
            while($data = fgetcsv($file)){
                array_push($cve,$data);
//    array_push($vv1,$data);
            }
        }
    else if($get == 'CNVD'){
        $file = fopen('CNVD.csv','r');
        while($data = fgetcsv($file)){
            array_push($cve,$data);
//    array_push($vv3,$data);
        }
    }
    else{
        header('location:index.php');
    }
}
//不分类的话就直接首页，都混在一起
else{
        $file = fopen('CVE.csv', 'r');
        while ($data = fgetcsv($file)) {
            if (empty($data[0])) {
                continue;
            }
            $data[4] = $data[0];
            array_push($cve, $data);
            //array_push($vv2,$data);
        }
        $file = fopen('seebug.csv','r');
        while($data = fgetcsv($file)){
            array_push($cve,$data);
        //array_push($vv1,$data);
        }

        $file = fopen('CNVD.csv','r');
        while($data = fgetcsv($file)){
            array_push($cve,$data);
        }
        //array_push($vv3,$data);

}



$cve = array_reverse($cve);

//按照时间排序
foreach ($cve as $key=>$value){
    $cve[$key][1] = @date('Ymd',strtotime($value[1]));
    $datetime = array();
}

foreach ($cve as $u) {
    $datetime[] = $u[1];
}

array_multisort($datetime,SORT_DESC,$cve);
//print_r($cve);
fclose($file);
//foreach ($cve as $a){
//    foreach ($a as $b){
//        echo $b . '<br>';
//    }
//}

$num = count($cve);//总的文章数
$curr = isset($_GET['page']) ? $_GET['page'] : 1;
$cnt = 5;//每页显示条数
$cve = array_slice($cve,($curr-1)*$cnt,$cnt);
$max = ceil($num / $cnt);//总的页数
$page = getPage($num,$curr,$cnt);

require ('moban.html');
?>
